﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
* Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * Website  : http://www.wishenterprise.com
 * 
 * */

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using QWEntity.Local;
using QWRepository;
using QWRepository.Local;

namespace QWControlCenter
{
    /// <summary>
    /// CompanyUI
    /// </summary>
    public partial class CompanyUI : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CompanyUI"/> class.
        /// </summary>
        public CompanyUI()
        {
            InitializeComponent();
            Initialize();
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        private void Initialize()
        {
            IDataAccess<Company> db = new CompanyBase();
            List<Company> companies = db.GetAll();

            if (companies.Count == 0) return;
            try
            {
                txtCompanyName.Text = companies[0].CompanyName;
                txtCompanyAddress.Text = companies[0].Address;
                txtCompanyCity.Text = companies[0].City;
                txtCompanyPhone.Text = companies[0].Phone;
                txtCompanyFax.Text = companies[0].Fax;
                txtCompanyEmail.Text = companies[0].Email;
                txtCompanyUrl.Text = companies[0].Url;
            }
            catch (NullReferenceException)
            {
            }
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            Company obj;
            try
            {
                obj =
                    new Company
                        {
                            CompanyName = txtCompanyName.Text.Trim(),
                            Address = txtCompanyAddress.Text.Trim(),
                            City = txtCompanyCity.Text.Trim(),
                            Phone = txtCompanyPhone.Text.Trim(),
                            Fax = txtCompanyFax.Text.Trim(),
                            Email = txtCompanyEmail.Text.Trim(),
                            Url = txtCompanyUrl.Text.Trim()
                        };
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message,
                                @"Warning!",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return;
            }

            if ((Confirm("Are you sure to update your company profile?") == DialogResult.Yes))
            {
                DialogResult dlg;
                using (var f = new LogonPresentation())
                {
                    dlg = f.ShowDialog(this);
                }

                if (dlg == DialogResult.OK)
                {
                    // --- TRUNCATE
                    var companyDao = new CompanyBase();
                    companyDao.Truncate();

                    // --- SAVE
                    if (companyDao.Save(obj) == 1)
                    {
                        Close();
                        Dispose();
                    }
                }
            }
        }

        /// <summary>
        /// Confirms the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        private DialogResult Confirm(string message)
        {
            DialogResult dialogResult =
                (MessageBox.Show(message,
                                 @"Confirm",
                                 MessageBoxButtons.YesNoCancel,
                                 MessageBoxIcon.Question));
            switch (dialogResult)
            {
                case DialogResult.No:
                    Close();
                    Dispose();
                    break;
            }
            return dialogResult;
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }

        /// <summary>
        /// Handles the TextChanged event of the txtCompanyName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtCompanyName_TextChanged(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = EnvironmentRepository.SuppressChar(((TextBox)sender).Text);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtCompanyAddress control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtCompanyAddress_TextChanged(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = EnvironmentRepository.SuppressChar(((TextBox)sender).Text);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtCompanyCity control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtCompanyCity_TextChanged(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = EnvironmentRepository.SuppressChar(((TextBox)sender).Text);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtCompanyPhone control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtCompanyPhone_TextChanged(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = EnvironmentRepository.SuppressChar(((TextBox)sender).Text);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtCompanyFax control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtCompanyFax_TextChanged(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = EnvironmentRepository.SuppressChar(((TextBox)sender).Text);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtCompanyEmail control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtCompanyEmail_TextChanged(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = EnvironmentRepository.SuppressChar(((TextBox)sender).Text);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtCompanyUrl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtCompanyUrl_TextChanged(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = EnvironmentRepository.SuppressChar(((TextBox)sender).Text);
        }
    }
}